package com.beijiteshop.shop.ui.mine

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.beijiteshop.libcommon.event.LoginEvent
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.libcommon.login.UserInfo
import com.beijiteshop.libcommon.login.UserInfoUtil
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.libcommon.utils.ImageLoadUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentMineBinding
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.address.fragment.AddressFragment
import com.beijiteshop.shop.ui.coupon.fragment.CouponFragment
import com.beijiteshop.shop.ui.login.LoginActivity
import com.beijiteshop.shop.ui.login.vm.UserVM
import com.beijiteshop.shop.ui.order.fragment.OrderTabFragment
import com.blankj.utilcode.util.ActivityUtils
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * 我的
 */
class MineFragment : BaseContainerFragment(R.layout.fragment_mine) {

    companion object{

        fun newInstance(): MineFragment{
            return MineFragment()
        }
    }

    private val mBinding by bindView<FragmentMineBinding>()
    private val mUserVM: UserVM by viewModel()

    override fun onLazyLoad() {

    }

    override fun initView(view: View) {
        val userinfo = UserInfoUtil.getInstance().getUserInfo()
        if (userinfo != null){
            setUserInfo(userinfo)
        }else{
            requestUserInfo()
        }
    }

    override fun initData() {

    }

    override fun initEvent() {
       mBinding.apply {
           //全部订单
           mineOrderAllTv.setOnClickListener {
               startOrderDetailActivity(0)
           }
           //待付款
           minePaidLl.setOnClickListener {
               startOrderDetailActivity(1)
           }
           //待发货
           mineDeliveredLl.setOnClickListener {
               startOrderDetailActivity(2)
           }
           //待收货
           mineReceivedLl.setOnClickListener {
               startOrderDetailActivity(3)
           }
           //交易关闭
           mineTransactionLl.setOnClickListener {
               startOrderDetailActivity(5)
           }

           //地址
           mineAddressLl.setOnClickListener {
               startActivity(PageIndex.ADDRESS,Bundle().apply {
                   putBoolean(AddressFragment.BUNDLE_IS_SELECT,false)
               })
           }

           //优惠券
           mineCouponLl.setOnClickListener {
               startActivity(PageIndex.COUPON,Bundle().apply {
                   putInt(CouponFragment.BUNDLE_TYPE,0)
               })
           }

           //退出登录
           mineLogoutLl.setOnClickListener {
               UserInfoUtil.getInstance().clear()
               ActivityUtils.startActivity(Intent(requireContext(),LoginActivity::class.java))
               ActivityUtils.finishOtherActivities(LoginActivity::class.java)
           }

           //设置
           mineSettingIv.setOnClickListener {
               startActivity(PageIndex.ACCOUNT_BINDING)
           }
       }

        //接收登录成功事件，更新用户信相关UI
        EventUtil.observe<Any>(this,LoginEvent.LOGIN_SUCCESS){
            val username = UserInfoUtil.getInstance().getLoginInfo()?.phone
            if (!username.isNullOrEmpty()){
                mBinding.mineUsernameTv.text = username
            }
        }

        //账号绑定成功,更新用户信相关UI
        EventUtil.observe<Any>(this,EventKey.ACCOUNT_BIND_SUCCESS){
            requestUserInfo()
        }

        mUserVM.apply {
            liveUserInfo.observeKt { userInfo ->
                userInfo?.let {
                    setUserInfo(it)
                }
            }
        }
    }

    private fun startOrderDetailActivity(index: Int){
        startActivity(PageIndex.ORDER_TAB,Bundle().apply {
            putInt(OrderTabFragment.BUNDLE_DEFAULT_INDEX,index)
        })
    }

    private fun requestUserInfo(){
        mUserVM.getUserInfo()
    }

    private fun setUserInfo(userinfo: UserInfo){
        val wxName: String? = userinfo.wxName
        if (wxName.isNullOrEmpty()){
            val phone = UserInfoUtil.getInstance().getLoginInfo()?.phone
            mBinding.mineUsernameTv.text = phone?:""
        }else{
            mBinding.mineUsernameTv.text = wxName
        }
        userinfo.headUrl?.let {
            ImageLoadUtil.loadCircle(it,mBinding.minePortraitIv)
        }
    }
}